package com.zjp.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zjp.pojo.Vaccines;
import com.zjp.service.VaccineService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * 疫苗管理-疫苗录入模块
 */
@RestController
@Slf4j
public class VaccinesController {
    @Autowired
    private VaccineService vaccineService;

    /**
     * 查询所有疫苗信息
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequestMapping("/findAllVaccines")
    public ModelAndView findAllVaccines(@RequestParam(name = "pageNo", required = false, defaultValue = "1") int pageNo,
                                        @RequestParam(name = "pageSize", required = false, defaultValue = "10") int pageSize) {
        //声明一个ModelAndView对象
        ModelAndView modelAndView = new ModelAndView();
        //开启分页
        PageHelper.startPage(pageNo, pageSize);   //开启分页
        //查询所有疫苗信息
        List<Vaccines> vaccinesList = vaccineService.findAllVaccines();
        //将查询到的疫苗信息添加到ModelAndView中
        modelAndView.addObject("vaccinesList", vaccinesList);
        //将分页信息添加到ModelAndView中
        PageInfo pageInfo = new PageInfo<>(vaccinesList);  //存储分页信息
        modelAndView.addObject("pageInfo", pageInfo);
        //设置视图名称
        modelAndView.setViewName("vaccinesInfo");
        log.info("查询到的疫苗信息为：{}", vaccinesList);
        //返回ModelAndView
        return modelAndView;
    }

    /**
     * 新增疫苗
     */
    @RequestMapping("/addVacc")
    public String addVacc(Vaccines vaccines) {
        int i = vaccineService.addVacc(vaccines);
        log.info("新增疫苗结果为：{}", i);
        if (i > 0) {
            return "SUCCESS";
        }
        return "error";
    }

    /**
     * 修改疫苗
     */
    @RequestMapping("/updateVacc")
    public int updateVacc(Vaccines vaccines) {
        int i = vaccineService.updateVacc(vaccines);
        log.info("修改疫苗结果为：{}", i);
        return i;
    }

    /**
     * 删除疫苗
     */
    @RequestMapping("/deleteVaccById")
    public String deleteVaccById(Long vaccinesid){
        int i=vaccineService.deleteVaccById(vaccinesid);
        log.info("删除疫苗结果为：{}",i);
        if(i>0){
            return "SUSSES";
        }
        return "FAIL";
    }

    /**
     * //疫苗管理-疫苗录入-接种点-请选择录入的疫苗
     */
    //查询所有疫苗信息
    @GetMapping("/findAllVacc")
    public List<Vaccines> findAllVacc(){
        List<Vaccines> allVaccines = vaccineService.findAllVaccines();
        log.info("查询疫苗管理-疫苗录入-接种点信息为：{}", allVaccines);
        return allVaccines;
    }
}
